package e.g.com.controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import e.g.com.dao.BaseDao;
import e.g.com.dao.BaseDaoImpl;
import e.g.com.pojo.Logi;
import e.g.com.pojo.Tb_user;
import e.g.com.service.UserService;

@Controller
public class LoginConteoller {
	@Resource
	private UserService uss;
	private Tb_user user=new Tb_user();
	@Resource
	private BaseDao<Tb_user> bd;
	@RequestMapping(value="/mlogin",method=RequestMethod.POST)
	public String mlogin(Logi us,HttpSession session,HttpServletResponse respons,HttpServletRequest request){
		System.out.println(us.getUser()+"  "+us.getPass());
//		user.setId(1);
//		user.setName("sx");
//		user.setPassword("007");
//		user.setRole("admin");
//		
//		uss.insert(user);
		session.setAttribute("user", us.getUser());
		
		user.setName(us.getUser());
		user.setPassword(us.getPass());
		if(uss.find(user).size()>0){
			user=uss.find(user).get(0);
			String rol=user.getRole();
			session.setAttribute("role", rol);
			session.setMaxInactiveInterval(3600);
			Cookie cookie=new Cookie("name", user.getName());
			Cookie cookie2=new Cookie("pass", us.getPass());
			cookie2.setMaxAge(200);
			cookie2.setPath("/");
			cookie.setMaxAge(200);
			cookie.setPath("/");
			respons.addCookie(cookie);
			respons.addCookie(cookie2);
			if(rol.equals("admin"))
				return "/admin/aindex";
			else if(rol.equals("manage"))
				return "/manage/mindex";
			}
		else
			return "/index";
	 return "/index";
	

	}
}
